package utils;

import config.EnvConfig;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.testng.annotations.DataProvider;

import java.io.InputStream;

public class ExcelDataProvider {
    @DataProvider(name = "Testdata")
    public static Object[][] getTestdata() throws Exception {
        try (InputStream is = ExcelDataProvider.class.getResourceAsStream(EnvConfig.TEST_DATA)) {
            Workbook workbook = WorkbookFactory.create(is);
            Sheet sheet = workbook.getSheet("Testdata"); // 修改 Sheet 名称

            int rowCount = sheet.getPhysicalNumberOfRows();
            Object[][] data = new Object[rowCount - 1][3]; // 假设标题行占第 1 行

            for (int i = 1; i < rowCount; i++) { // 跳过标题行
                Row row = sheet.getRow(i);
                if (row == null) continue;

                // 用户名
                data[i-1][0] = row.getCell(0) != null ?
                        row.getCell(0).getStringCellValue() : "";

                // 密码
                data[i-1][1] = row.getCell(1) != null ?
                        row.getCell(1).getStringCellValue() : "";

                // 预期结果（处理布尔值）
                data[i-1][2] = row.getCell(2) != null ?
                        row.getCell(2).getBooleanCellValue() : false;
            }
            return data;
        } catch (Exception e) {
            throw new RuntimeException("加载测试数据失败: " + e.getMessage(), e);
        }
    }
}